<?php declare(strict_types=1);


namespace App\Services\Refund\Strategy;

use App\Api\Controllers\Config\LeshuaUnionPayConfigController;
use App\Api\Controllers\LeshuaUnion\PayController;
use App\Exceptions\BizException;
use App\Models\Store;
use App\Services\Pay\GuoTonXinYi\Config;
use App\Services\Pay\GuoTonXinYi\Pay;

class GtxyRefundStrategy implements RefundStrategyInterface
{

    public function refundQuery(array $data): array
    {
        $store = Store::query()->where("store_id", $data["store_id"])->first();

        if (empty($store)) {
            throw new BizException("门店不存在");
        }

        list($mchNo, $config) = Config::config($store->store_id, "1234", $data["company"]);

        $result = Pay::refundQuery($data["refund_no"], [
            "agentId" => $config->platform_id,
            "custId" => $mchNo,
            "public_key" => $config->public_key
        ]);

        if ($result->getCode() != 1) {
            throw new BizException($result->getMsg());
        }
        $other_refund_no = $refund_detaiil = $refund_time = "";
        if ($result->getData()["pay_status"] == 6) {
            $status = 1;
            $other_refund_no = $result->getData()["other_refund_no"] ?? "";
            $refund_detaiil = $result->getData()["refund_detaiil"] ?? "";
            $refund_time = empty($result->getData()["refund_time"]) ? date("Y-m-d H:i:s", time()) : date("Y-m-d H:i:s", strtotime($result->getData()["refund_time"]));
        } else if ($result->getData()["pay_status"] == 7) {
            $status = 0;
        } else {
            $status = -1;
        }
        return [
            "status" => $status,
            "other_refund_no" => $other_refund_no,
            "refund_detaiil" => $refund_detaiil,
            "refund_time" => $refund_time
        ];
    }

}